package com.bktech.stupidnews.controlpanel;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

import com.bktech.fragment.ViewFragment;
import com.bktech.stupidnews.MainActivity;
import com.bktech.stupidnews.R;

public class HomePanel extends Fragment {

	MainActivity activity;
	ExpandableListView expanderListView;
	MyExpanderListAdapter adapter;
	ArrayList<Category> data;
	OnGroupClickListener onGroupClickListener;
	OnChildClickListener onChildClickListener;
	OnGroupCollapseListener onGroupCollapseListener;
	OnGroupExpandListener onGroupExpandListener;
	private boolean initExpandList = false;

	public HomePanel(MainActivity activity, ArrayList<Category> data) {
		this.activity = activity;
		this.data = data;
		adapter = new MyExpanderListAdapter(activity, data);

		onGroupClickListener = new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {

				if (adapter.getCategory(groupPosition).getType() == Category.TYPE.BUTTON_HOME) {
					return true;
				} else if (adapter.getCategory(groupPosition).getType() == Category.TYPE.BUTTON_ADD) {
					HomePanel.this.activity.openSearchPanel();
					return true;
				} else if (adapter.getCategory(groupPosition).getType() == Category.TYPE.BUTTON_EDIT) {
					HomePanel.this.activity.addSetingPanel(new EditPanel(
							HomePanel.this.activity, HomePanel.this.data));
					return true;
				} else {
					return false;
				}

			}
		};

		onChildClickListener = new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				ChildCategory rss = adapter.getCategory(groupPosition).listChild
						.get(childPosition);
				if (rss.rssId.equals(HomePanel.this.activity.viewFragment.id)) {
					Toast.makeText(HomePanel.this.activity,
							"You are reading " + rss.title, Toast.LENGTH_SHORT)
							.show();
					HomePanel.this.activity.closeSettingPanel();
					return false;
				}
				HomePanel.this.activity.addView(new ViewFragment(
						HomePanel.this.activity, rss.rssId, rss.title,
						rss.imageUrl));
				HomePanel.this.activity.closeSettingPanel();
				return false;
			}
		};

		onGroupCollapseListener = new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
				// TODO Auto-generated method stub
				adapter.getCategory(groupPosition).expand = false;

			}
		};

		onGroupExpandListener = new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				adapter.getCategory(groupPosition).expand = true;

			}
		};

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		expanderListView = (ExpandableListView) inflater.inflate(
				R.layout.seting_panel_home, container, false);
		expanderListView.setAdapter(adapter);
		if (!initExpandList) {
			initExpandList = true;
			for (int i = 0; i < adapter.listCategory.size(); i++) {
				if (adapter.getCategory(i).expand) {
					expanderListView.expandGroup(i);
				}
			}
		}
		expanderListView.setOnGroupClickListener(onGroupClickListener);
		expanderListView.setOnChildClickListener(onChildClickListener);
		expanderListView.setOnGroupCollapseListener(onGroupCollapseListener);
		expanderListView.setOnGroupExpandListener(onGroupExpandListener);
		return expanderListView;
	}

}
